package com.dfec.servlet;

import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.model.Msg;
import com.dfec.model.News;
import com.dfec.service.INewsService;
import com.dfec.service.imp.NewsServiceImp;
import com.dfec.utils.Consists;
import com.dfec.utils.ResponseUtil;

/**
 * Servlet implementation class NewsServlet
 */
@WebServlet(name = "news", description = "新闻接口", urlPatterns = { "/news" })
public class NewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Msg msg=null;
    List<Object> list2=null;
    INewsService newsService=null;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewsServlet() {
        super();
        newsService=new NewsServiceImp();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		  list2= new ArrayList<>();
		  msg=new Msg();
		//获取新闻列表、检索1，2获取详情，3，删除新闻,4新增新闻5.点赞 6.浏览量
		if(request.getParameter("action").equals("1")) {
			getNewsList(request, response);
			ResponseUtil.sendScript(response, JSON.toJSONString(list2));
		}else if(request.getParameter("action").equals("2")) {
			getNewsContent(request, response);
			ResponseUtil.sendScript(response, JSON.toJSONString(list2));
		}else if(request.getParameter("action").equals("3")) {
			getNewsDelete(request, response);
			ResponseUtil.sendScript(response, JSON.toJSONString(list2));
		}else if(request.getParameter("action").equals("4")) {
			doPost(request, response);
		}
		
	}


	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		list2= new ArrayList<>();
		msg=new Msg();
		getNewsAdd(req, resp);
		ResponseUtil.sendScript(resp, JSON.toJSONString(list2));
	}

	private void getNewsAdd(HttpServletRequest request, HttpServletResponse response) {
		List<News> newsList=new ArrayList<>();
		News news=new News();
		 
		boolean re=true;
		try {
			news.setTitle(request.getParameter("title"));
			news.setContent(request.getParameter("content"));
			news.setContent_type(request.getParameter("content_type"));
			news.setIndex_pic(request.getParameter("index_pic"));
			news.setNews_type(request.getParameter("news_type"));
			news.setTag(request.getParameter("tag"));
			news.setSummary("【摘要】"+request.getParameter("summary"));
			news.setCarousel(request.getParameter("carousel"));
		    newsList.add(news);
		
			 re = newsService.saveBatchNews(newsList);
			 if(re) {
					msg.setCode(Consists.Error_Code_OK);
					msg.setMsg(Consists.Error_Msg_HANDLER_OK);
				}else {
					msg.setCode(Consists.Error_Code_FAIL);
					msg.setMsg(Consists.Error_Msg_HANDLER_FAIL);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				msg.setCode(Consists.Error_Code_ServerError);
				msg.setMsg(Consists.Error_Msg_ServerError);
			}
		list2.add(msg);
	}

	private void getNewsDelete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub  eg：1,2,3
	
		boolean result;
		try {
			String[] arr = request.getParameter("ids").split(",");
			result=newsService.deleteBatchNewsbyId(arr);
			if(!result) {
				msg.setCode(Consists.Error_Code_FAIL);
				msg.setMsg(Consists.Error_Msg_HANDLER_FAIL);
			}else {
				msg.setCode(Consists.Error_Code_OK);
				msg.setMsg(Consists.Error_Msg_HANDLER_OK);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
		}
		list2.add(msg);
	}

	private void getNewsContent(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		News newsId = new News();
		News news=null;
		
		try {
			newsId.setId(Integer.parseInt(request.getParameter("id")));
			news = newsService.getNewsContent(newsId,request.getParameter("phone"));
			if(news==null) {
				msg.setCode(Consists.Error_Code_FAIL);
				msg.setMsg(Consists.Error_Msg_FAIL);
			}else {
				msg.setCode(Consists.Error_Code_OK);
				msg.setMsg(Consists.Error_Msg_OK);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
		}
		list2.add(msg);
		list2.add(news);
	}

	private void getNewsList(HttpServletRequest request, HttpServletResponse response) {
		News news=new News();
		
		List<News> list=null,topNewsList=null;
		
		try {// String page, String size
			String numStr=request.getParameter("page");
			String tonumStr=request.getParameter("size");
			
			news.setContent(request.getParameter("content"));
			news.setTitle(request.getParameter("title"));
			news.setContent_type(request.getParameter("tontent_type"));
			news.setNews_type(request.getParameter("news_type"));
			news.setTag(request.getParameter("tag"));
			list = newsService.getNewsListByConditions(news, numStr, tonumStr);
			if (list.size()==0) {
				msg.setCode(Consists.Error_Code_FAIL);
				msg.setMsg(Consists.Error_Msg_FAIL);
			}else {
				msg.setCode(Consists.Error_Code_OK);
				msg.setMsg(Consists.Error_Msg_OK);
			}
			
			topNewsList=newsService.getNewsListCarouselTop5();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
		}
		list2.add(msg);
		list2.add(list);
		list2.add(topNewsList);
		
	}

}
